package com.smzd.framework.oauth2.business;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.smzd.framework.security.domain.Authority;
import com.smzd.framework.system.business.UserService;
import com.smzd.framework.system.domain.User;

public class OAuth2UserDetailsService implements UserDetailsService {
    private final static List<GrantedAuthority> authorities = Arrays.asList((GrantedAuthority) new Authority(
            "ROLE_USER"));

    @Autowired
    UserService service;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("-->loadUserByUsername('" + username + "'):");
        User user = service.getUserByUserName(username);
        System.out.println("-->loadUserByUsername:" + user);
        if (user != null) {
            user.setAuthorities(authorities);
        }
        return user;
    }

}
